import store from '@/store'

function checkPermission(el, binding) {
    const { value } = binding
    const permissions = store.getters && store.getters.permissions

    if (value) {
        const permissionChar = value
        var havePermission = permissions.some((p) => {
            // * 代表全部权限
            return p.permissionFlag === '*' ||
             p.permissionFlag === permissionChar
        })
        if (!havePermission) {
            el.parentNode && el.parentNode.removeChild(el)
        } else {
            //need not to do anthing
        }
    }

}

export default function install(Vue, options) {
    Vue.directive('permission', {
        inserted(el, binding) {
            checkPermission(el, binding)
        },
        update(el, binding) {
            checkPermission(el, binding)
        }
    })
}

